package com.dsg.controller.set;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.apply.DsgApplyCar;
import com.dsg.entity.model.set.DsgSetCarBacklistModel;
import com.dsg.entity.set.DsgSetCarBacklist;
import com.dsg.service.apply.IDsgApplyCarService;
import com.dsg.service.set.IDsgSetCarBacklistService;
import com.dsg.support.LeveldbAccess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 車輛黑名單 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/set/dsgSetCarBacklist")
@Api(description = "車輛黑名單")
public class DsgSetCarBacklistController extends BaseController {

    @Autowired
    private IDsgSetCarBacklistService service;
    @Autowired
    private IDsgApplyCarService dsgApplyCarService;
    @Autowired
    private LeveldbAccess leveldbAccess;

    /**
     * 查询列表（分页）
     *
     * @param dsgSetCarBacklist
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgSetCarBacklist dsgSetCarBacklist) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgSetCarBacklist.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgSetCarBacklist);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 新增黑名單-車
     *
     * @param dsgSetCarBacklist
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增黑名單-車")
    public AjaxResult add(@RequestBody DsgSetCarBacklist dsgSetCarBacklist) {
        dsgSetCarBacklist.setOid(UUIDGenerator.getUUID());
        //查詢客戶資料
        QueryWrapper<DsgApplyCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CAR_NO", dsgSetCarBacklist.getCarNo());
        DsgApplyCar dsgApplyCar = dsgApplyCarService.getOne(queryWrapper, false);
        if (dsgApplyCar != null) {
            dsgSetCarBacklist.setCarId(dsgApplyCar.getOid());
        }
        boolean flag = service.save(dsgSetCarBacklist);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }
    /**
     * 更新
     *
     * @param dsgSetCarBacklist
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgSetCarBacklist dsgSetCarBacklist) {
        AjaxResult ajaxResult;
        boolean flag = service.updateById(dsgSetCarBacklist);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgSetCarBacklist);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = service.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 上載
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation("上載")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importExcel(@ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file) throws Exception {
        EasyExcel.read(file.getInputStream(), DsgSetCarBacklistModel.class, new UploadDsgSetCarBacklist(service, dsgApplyCarService, leveldbAccess)).sheet(0).headRowNumber(1).doRead();
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }
}
